//
//  TCSGgetLitAuthor.m
//  iPing
//
//  Created by Thomas on 8/16/09.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "TCSGgetLitStory.h"


@implementation TCSGgetLitStory
-(id)init
{
	self=[super init];
	running = YES;
	NSNotificationCenter *nc = [NSNotificationCenter defaultCenter]; 
	[nc addObserver:self 
           selector:@selector(setRunning:) 
               name:TCSGRunning
             object:nil]; 
	storyArray = [[NSArray alloc] init];
	return self;
}
-(void)setStoryList:(id)storyList;
{
	theStoryList = storyList;
	NSLog(@"%@",[theStoryList getLastTitle]);
}
-(void)setRunning:(NSNotification *)note
{
	running = [[note object] boolValue];
}
- (NSArray *)getStoriesArrayForAuthor:(NSString *)authorID
{
	NSLog(@"starting authors");
	NSString *tempString = [NSString stringWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.literotica.com/stories/memberpage.php?uid=%@&page=submissions",authorID,nil]]];
	NSArray *tempArray = [tempString componentsSeparatedByString:@"href=\"http://www.literotica.com/stories/showstory.php?id="];
	int i;
	NSMutableArray *tempArray2 = [[NSMutableArray alloc] init];
	for (i=1;i<[tempArray count];i++)
	{
		NSRange tempRange = [[tempArray objectAtIndex:i] rangeOfString:@"\">"];
		[tempArray2 addObject:[[tempArray objectAtIndex:i] substringToIndex:tempRange.location]];
	}
	NSLog(@"tempArray2:%@",tempArray2);
	return tempArray2;	
}
-(NSDictionary *)getStory:(NSString *)storyID
{
	
	NSString *page = [NSString stringWithContentsOfURL:
					  [NSURL URLWithString:
					   [NSString stringWithFormat:@"http://www.literotica.com/stories/showstory.php?id=%@",storyID,nil]]];
	[page writeToFile:@"/Users/tom/Desktop/1.html" atomically:YES];
	NSRange titleStart		= [page rangeOfString:@"<title>"];
	NSRange titleEnd		= [page rangeOfString:@"</title>"];
	NSRange titleRange;
	titleRange.location		= titleStart.location+titleStart.length;
	titleRange.length		= titleEnd.location-titleRange.location;
	NSArray *titleArray		= [[page substringWithRange:titleRange] componentsSeparatedByString:@" - "];
	//NSLog(@"titleArray:%@",titleArray);
	NSString *genre			= [titleArray objectAtIndex:1];
	NSString *host			= [titleArray objectAtIndex:2];
	NSString *title			= [titleArray objectAtIndex:0];
	
	NSRange authorStart		= [page rangeOfString:@"submissions\">"];
	NSString *authorString  = [page substringFromIndex:(authorStart.location + authorStart.length)];
	NSRange authorEnd		= [authorString rangeOfString:@"</a><span>"];
	NSString *author		= [authorString substringToIndex:(authorEnd.location)];
	NSLog(@"111");
	NSString *story = [self getLITStoryProper:storyID withPageOne:page];
	NSLog(@"222");
	story = [NSString stringWithFormat:@"<html><head><title>%@</title></head><body><h1 class=\"chapter\">%@</h1>\n%@</body></html>",title,title,story,nil];
	NSMutableDictionary *returnDicts = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
										//story,STORYKEY,
										title,TITLEKEY,
										author,AUTHORKEY,
										host,HOSTKEY,
										storyID, IDKEY,
										[NSNumber numberWithBool:YES],CHECKKEY,
										genre,GENREKEY,
										storyArray, ARRAYKEY,
										nil];
	//[theStoryList printDict];
	NSLog(@"returnDict:%@",returnDicts);
	[returnDicts setObject:story		forKey:STORYKEY];
	
	
	//<div id="content">
	
	return returnDicts;
}

- (NSString *)getLITStoryProper:storyID withPageOne:page
{
	NSRange storyStart	= [page rangeOfString:@"<div id=\"content\">"];
	NSRange pageEnd		= [page rangeOfString:@"<div id=\"storypager\">"];
	NSString *story		=@"multiple";
	NSMutableArray *	pagesArray = [[NSMutableArray alloc] init];
	if(pageEnd.location ==NSNotFound)
	{
		NSRange tempRange	= {storyStart.location,([page length]-storyStart.location)};
		/*tempRange.location = storyStart.location
		 tempRange.length*/
		pageEnd				= [page rangeOfString:@"<div class=" options:NSCaseInsensitiveSearch range:tempRange ];
		NSRange storyRange	= {(storyStart.location+storyStart.length),(pageEnd.location-storyStart.location-storyStart.length) };
		story= [page substringWithRange:storyRange];
		[pagesArray addObject:story];
	}
	else
	{
		NSRange storyRange		= {(storyStart.location+storyStart.length),(pageEnd.location-storyStart.location-storyStart.length) };
		story = [page substringWithRange:storyRange];
		//Need to get storyParser
		NSMutableArray * pagesArray = [[page componentsSeparatedByString:[NSString stringWithFormat:@"<a href=\"http://www.literotica.com/stories/showstory.php?id=%@&amp;page=",storyID,nil]] mutableCopy];
		[pagesArray removeObjectAtIndex:0];
		int i;
		for(i=0;i<[pagesArray count];i++)
		{
			NSRange tempRange = [[pagesArray objectAtIndex:i] rangeOfString:@"\">"];
			NSRange tempRange2 = {0,tempRange.location};
			[pagesArray replaceObjectAtIndex:i withObject:[[pagesArray objectAtIndex:i] substringWithRange:tempRange2]];
		}
		[pagesArray removeObjectAtIndex:0];
		for(i=0;i<[pagesArray count];i++)
		{
			NSString *pageLater = [NSString stringWithContentsOfURL:
								   [NSURL URLWithString:
									[NSString stringWithFormat:@"http://www.literotica.com/stories/showstory.php?id=%@&page=%@",storyID,[pagesArray objectAtIndex:i],nil]]];
			NSRange pageLaterStart	= [pageLater rangeOfString:@"<div id=\"content\">"];
			NSRange pageLaterEnd	= [pageLater rangeOfString:@"<div id=\"storypager\">"];
			NSRange PageLaterRange	= {(pageLaterStart.location+pageLaterStart.length),(pageLaterEnd.location-pageLaterStart.location-pageLaterStart.length) };
			//NSLog(@"1");
			pageLater = [pageLater substringWithRange:PageLaterRange];
			story=[NSString stringWithFormat:@"%@\n\n<br>%@",story,pageLater,nil];
			//NSLog(@"2");
			[pagesArray addObject:pageLater];
		}
		//[story writeToFile:[[@"/Users/tom/Desktop/newFolder" stringByAppendingPathComponent:storyID] stringByAppendingPathExtension:@"html"] atomically:YES];
		
	} 
	storyArray = [NSArray arrayWithArray:pagesArray];
	return story;
}
- (NSDictionary *)getMoreInfoForID:(NSString *)storyID
{
	NSString *page = [NSString stringWithContentsOfURL:
					  [NSURL URLWithString:
					   [NSString stringWithFormat:@"http://www.literotica.com/stories/showstory.php?id=%@",storyID,nil]]];
	
	NSRange descStart	= [page rangeOfString:@"<meta name=\"description\" content=\""];
	NSRange tempRange	= {descStart.location+descStart.length,([page length]-(descStart.location+descStart.length))};
	NSRange descEnd		= [page rangeOfString:@"\"  />" options:NSCaseInsensitiveSearch range:tempRange];
	NSRange descRange	= {(descStart.location+descStart.length),(descEnd.location-(descStart.location+descStart.length))};
	
	NSMutableDictionary *extraInfo	= [[NSMutableDictionary alloc] initWithObjectsAndKeys:[page substringWithRange:descRange],SUMMARYKEY,nil];
	return extraInfo;
}

@end
